home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / magict_1 / form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-07-22  |  11.0 KB  |  338 lines

  1. VERSION 5.00
  2. Object = "{3035B5D2-295D-11D3-8C54-006008BA8D16}#1.0#0"; "MAGICTCP.OCX"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   9360
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   13830
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   9360
  11.    ScaleWidth      =   13830
  12.    StartUpPosition =   3  'Windows-Standard
  13.    WindowState     =   2  'Maximiert
  14.    Begin VB.CommandButton txtEnd 
  15.       Caption         =   "End"
  16.       Height          =   495
  17.       Left            =   1680
  18.       TabIndex        =   2
  19.       Top             =   120
  20.       Width           =   1335
  21.    End
  22.    Begin VB.CommandButton cmdClear 
  23.       Caption         =   "Clear"
  24.       Height          =   495
  25.       Left            =   240
  26.       TabIndex        =   1
  27.       Top             =   120
  28.       Width           =   1335
  29.    End
  30.    Begin VB.TextBox txtInfo 
  31.       BeginProperty Font 
  32.          Name            =   "Courier New"
  33.          Size            =   8.25
  34.          Charset         =   0
  35.          Weight          =   400
  36.          Underline       =   0   'False
  37.          Italic          =   0   'False
  38.          Strikethrough   =   0   'False
  39.       EndProperty
  40.       Height          =   8595
  41.       Left            =   240
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   3  'Beides
  44.       TabIndex        =   0
  45.       Top             =   720
  46.       Width           =   11415
  47.    End
  48.    Begin M3LibCtl.MagicTCP Magic 
  49.       Left            =   3960
  50.       OleObjectBlob   =   "Form1.frx":0000
  51.       Top             =   120
  52.    End
  53.    Begin VB.Label Label1 
  54.       Alignment       =   1  'Rechts
  55.       Caption         =   "Copyright 1999, hiNRGware, All rights reserved!"
  56.       BeginProperty Font 
  57.          Name            =   "MS Sans Serif"
  58.          Size            =   8.25
  59.          Charset         =   0
  60.          Weight          =   700
  61.          Underline       =   0   'False
  62.          Italic          =   0   'False
  63.          Strikethrough   =   0   'False
  64.       EndProperty
  65.       Height          =   375
  66.       Left            =   6840
  67.       TabIndex        =   3
  68.       Top             =   240
  69.       Width           =   4575
  70.    End
  71. Attribute VB_Name = "Form1"
  72. Attribute VB_GlobalNameSpace = False
  73. Attribute VB_Creatable = False
  74. Attribute VB_PredeclaredId = True
  75. Attribute VB_Exposed = False
  76. Option Explicit
  77. Const CONN_CLIENT = 1   ' Connection from client (web browser)
  78. Const CONN_SERVER = 2   ' connection to web server / proxy server
  79. ' Shall we connect indirectly via a proxy server (cascaded proxies)?
  80. Dim ProxyServer As String
  81. Dim ProxyPort As Long
  82. Private Sub Cleanup()
  83. With Magic
  84.     .Delete .CurrentSocket
  85. End With
  86. End Sub
  87. Private Sub Log(Evt As String)
  88. Dim i As Integer
  89. Dim s As String
  90. s = txtInfo & "[" & Magic.CurrentSocket & "]" & Evt & vbCrLf
  91. While Len(s) > 32000
  92.     i = InStr(s, vbCrLf)
  93.     If (i > 0) Then
  94.         s = Mid(s, i + Len(vbCrLf))
  95.     Else
  96.         s = ""
  97.     End If
  98. txtInfo = s
  99. txtInfo.SelStart = Len(s) + 1
  100. End Sub
  101. Private Sub cmdClear_Click()
  102. txtInfo = ""
  103. End Sub
  104. Private Sub Form_Load()
  105. Dim tf As Boolean 'tf means True/False
  106. With Magic
  107.     .LogEnable = True
  108.     .LogFile = "C:\TEMP\MAGICTCP.TXT"
  109.     'Socket 0 is created automatically.
  110.     ' So we cab start right away by setting up the listening socket :)
  111.     ' Check if a proxy is specfied, where this proxy should connect to (casciding)
  112.     ProxyServer = .GetProfileString("PROXY", "PROXYSERVER", "")
  113.     ProxyPort = .GetProfileInt("PROXY", "PROXYPORT", 0)
  114.     ' Listen on port 8080 (by defualt) for incoming connection requests
  115.     .LocalPort = .GetProfileInt("PROXY", "PORT", 8080)
  116.     .ReUseAddr = True
  117.     ' Let's listen
  118.     tf = .Listen
  119.     ' Something went wrong?
  120.     If Not tf Then
  121.         MsgBox "Listen_Error: " & .LastErrorText
  122.     End If
  123. End With
  124. End Sub
  125. Private Sub Magic_OnAccept(ByVal ListenSocket As Long)
  126. With Magic
  127.     ' new connection accepted. don't do anything here. Wait for data to arrive! See OnRead()
  128.     .zzType = CONN_CLIENT
  129.     ' no data from client read so far
  130.     .zzBuffer = ""
  131.     .LogSocket = True
  132. End With
  133. End Sub
  134. Private Sub Magic_OnClose()
  135. With Magic
  136.     ' delete current socket and it's partner socket
  137.     .Delete .CurrentSocket
  138. End With
  139. End Sub
  140. Private Sub Magic_OnConnect()
  141. ' successfull connected. wait for the OnWrite() event to write data to distant host.
  142. End Sub
  143. Private Sub Magic_OnError(ByVal WinsockError As Long, ByVal Func As String)
  144. With Magic
  145.     ' Don't care for asyncronous errors
  146.     '
  147.     ' delete current socket and it's partner socket
  148.     .Delete .CurrentSocket
  149. End With
  150. End Sub
  151. Private Sub Magic_OnRead()
  152. ' data from client has arrived!
  153. ' the web browser send an http requests, it's first line has the following structure:
  154. ' OPERATION http://hostname.anywhere.com/path/file?param=dontcare HTTP/1.x <cr> <lf>
  155. ' we'll connect to specified host, and route the rest of the request and the full response
  156. Dim tf As Boolean
  157. Dim take As Boolean
  158. Dim s As String
  159. Dim i As Integer
  160. Dim currSock As Long
  161. Dim newSock As Long
  162. Dim hostname As String
  163. Dim hostport As Long
  164. With Magic
  165.     tf = True                       ' no error so far
  166.     take = False                    ' first line is not complete yet
  167.     newSock = -1                    ' no new socket created
  168.     currSock = .CurrentSocket       ' remember number of current socket
  169.     If tf Then
  170.         ' try to read the first line
  171.         If Not .IsValidSocket(currSock) Then
  172.             MsgBox "Error!!!!!!"
  173.         End If
  174.         
  175.         If Not .ReadString(s) Then
  176.             tf = False
  177.             Log "Read-Error: " & .LastErrorText
  178.         End If
  179.     End If
  180.     If tf Then
  181.         ' try to isolate the first line which is terminated by <CR> <LF>
  182.         s = .zzBuffer & s
  183.         i = InStr(s, vbCrLf)
  184.         If i > 0 Then
  185.             take = True                     ' first line is complete
  186.             hostname = Left$(s, i - 1)
  187.         Else
  188.             ' first line is not complete. wait for the the next read event. data already received
  189.             ' is stored in the socket's zzBuffer
  190.             .zzBuffer = s
  191.         End If
  192.     End If
  193.     If (tf And take) Then
  194.         ' let's log the request on the user interface
  195.         Log .RemoteHost & ":" & .RemotePort & " " & hostname
  196.         
  197.         ' no proxy server for our operation definied?
  198.         If (Len(ProxyServer) = 0) Then
  199.             ' extract the hostname (and port) from the HTTP-request
  200.             
  201.             i = InStr(1, hostname, " ")
  202.             If (i > 0) Then
  203.                 hostname = LTrim$(Mid$(hostname, i + 1))
  204.                 i = InStr(1, hostname, "//")
  205.                 
  206.                 If (i > 0) Then
  207.                     hostname = LTrim$(Mid$(hostname, i + 2))
  208.                     i = InStr(1, hostname, "/")
  209.                 End If
  210.                 
  211.                 If (i = 0) Then
  212.                     i = InStr(1, hostname, " ")
  213.                 End If
  214.             End If
  215.             
  216.             
  217.             If (i > 0) Then
  218.                 hostname = Left$(hostname, i - 1)
  219.                 i = InStr(1, hostname, ":")
  220.                 If (i = 0) Then
  221.                     hostport = 80
  222.                 Else
  223.                     hostport = CLng(Mid$(hostname, i + 1))
  224.                     hostname = Left$(hostname, i - 1)
  225.                 End If
  226.             Else
  227.                 tf = False
  228.             End If
  229.         Else
  230.             hostname = ProxyServer
  231.             hostport = ProxyPort
  232.         End If
  233.     End If
  234.     If (tf And take) Then
  235.         ' do not receive any more data until connected from client
  236.         .OnReadEvent = False
  237.         
  238.         ' create a new socket and define this socket as our partner socket
  239.         newSock = .New
  240.         .PartnerSocket = newSock
  241.         
  242.         ' switch to the new socket and set it's partner socket as well
  243.         .CurrentSocket = newSock
  244.         .PartnerSocket = currSock
  245.         .OnReadEvent = False
  246.         
  247.         ' store data already read with the new socket and mark it as outgoing connection
  248.         .zzBuffer = s
  249.         .zzType = CONN_SERVER
  250.                 
  251.         ' set destination for Connect()
  252.         .RemoteHost = hostname
  253.         .RemotePort = hostport
  254.         
  255.         ' Connect to remote host
  256.         If Not .Connect() Then
  257.             ' failure!
  258.             ' A WOULDBLOCK is ignored since this is nor real error.
  259.             If Not .WouldBlock Then
  260.                 'other error!
  261.                 tf = False
  262.                 Log "Connect-Error: " & .LastErrorText
  263.             End If
  264.         End If
  265.     End If
  266.     ' if an error occured, delete this socket (an it's partner)
  267.     If Not tf Then
  268.         .Delete currSock
  269.     End If
  270. End With
  271. End Sub
  272. Private Sub Magic_OnWrite()
  273. Dim tf As Boolean
  274. Dim s As String
  275. Dim a As Integer
  276. Dim b As Integer
  277. Dim c As Integer
  278. Dim currSock As Long
  279. With Magic
  280.     ' relevant only for outgoing connecetions.
  281.     ' we have to forward the data read so far from the client to the web server / proxy
  282.     ' after this, we can activate forwaring between client and server,
  283.     ' since we do not need to parse any data anymore
  284.     If .zzType = CONN_SERVER Then
  285.         tf = True       ' no error so far
  286.         s = .zzBuffer   ' data read so far
  287.         currSock = .CurrentSocket   ' remember current socket
  288.         
  289.         ' if the remote server is not a proxy, we have to drop the hostname from the HTTP-request
  290.         If tf And (Len(ProxyServer) = 0) Then
  291.             a = InStr(1, s, " ")
  292.             
  293.             If (a > 0) Then
  294.                 b = InStr(a + 1, s, "//")
  295.             End If
  296.             
  297.             If (b > 0) Then
  298.                 c = InStr(b + 2, s, "/")
  299.             End If
  300.             
  301.             If (c > 0) Then
  302.                 s = Left$(s, a) & Mid$(s, c)
  303.             Else
  304.                 tf = False
  305.             End If
  306.         End If
  307.         
  308.         If tf Then
  309.             ' write all client data read so far to the server
  310.             tf = .WriteStringEx(s)
  311.             If Not tf Then
  312.                 Log "Write-Error: " & .LastErrorText
  313.             End If
  314.         End If
  315.         
  316.         If tf Then
  317.             ' no error so far
  318.             ' let's start data forwarding between client and server
  319.             '.Forward .PartnerSocket, True
  320.             
  321.             ' forward all data to the partner socket
  322.             .ForwardSocket = .PartnerSocket
  323.             
  324.             ' switch to partner socket and forward all of it's incoming data back to the client
  325.             .CurrentSocket = .PartnerSocket
  326.             .ForwardSocket = .PartnerSocket
  327.         End If
  328.         
  329.         If Not tf Then
  330.             ' delete the current socket on error
  331.             .Delete currSock
  332.         End If
  333.     End If
  334. End With
  335. End Sub
  336. Private Sub txtEnd_Click()
  337. End Sub
  338.